# -*- coding: utf-8 -*-

import struct
import re
import os
import math

def float_to_hex_fp64(i: str) -> str:
    f = float(i)
    h = hex(struct.unpack('!Q', struct.pack('!d', f))[0])
    #print(str(h)[2:])
    return str(h)[2:]

def hex_fp64_to_float(hex_data: str) -> str:
    # 将十六进制表示的fp64类型数转换为浮点数
    hex_data = re.sub("_", "", hex_data)
    ori_data = struct.unpack('!d', bytes.fromhex(hex_data))[0]
    return str(ori_data)

def float_to_hex_fp16(i: str) -> str:
	f = float(i)
	#old_h = hex(struct.unpack('<H', struct.pack('<e', f))[0])
	#old_h = str(old_h)[2:]
	#第二种方式和第一种方式结果其实一样的
	packed_data  = struct.pack('!e', f)
	h = ''.join(format(byte, '02x') for byte in packed_data)
	#return str(h)[2:]
	return str(h)

def hex_fp16_to_float(hex_data: str) -> str:
	hex_data = re.sub("_", "", hex_data)
	ori_data = struct.unpack('!e', bytes.fromhex(hex_data))[0]
	return str(ori_data)

def float_to_hex(i: str) -> str:
	f = float(i)
	h = hex(struct.unpack('<I', struct.pack('<f', f))[0])
	return str(h)[2:]

def hex_to_float(hex_data: str) -> str: #in fp32: 3eb33333
	hex_data = re.sub("_", "", hex_data)
	ori_data = struct.unpack('!f', bytes.fromhex(hex_data))[0]
	return str(ori_data)

def dec_to_bin(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	return format(int(i),'b')#08b

def bin_to_dec(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	return str(int(str(i), 2))

def dec_to_bnr(i: int, lenth: int = 1) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	dec = int(i)
	digits = (len(bin(dec)) - 3 + 1) if dec < 0 else (len(bin(dec)) - 2)
	if digits >= lenth:
		lenth = digits
	pattern = f"{dec & int('0b' + '1' * lenth, 2):0{lenth}b}"
	return "".join(code for code in pattern)

def bnr_to_dec(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	for num in i:
		if num not in ["0", "1"]:
			raise ValueError("Not bin str")
	if i.startswith("0"):
		dec = int(i, 2)
	else:
		dec = int(i[1:], 2) - 0x01
		dec = -(~dec & int("0b" + "1" * (len(i) - 1), 2))
	return str(dec)

def dec_to_hex(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	if i.startswith("-"):
		i = re.sub("-", "", i)
		return "-" + str(hex(int(i)))[2:]
	else:
		return str(hex(int(i)))[2:]

def bin_to_bnr(i: str) -> str:
	return dec_to_bnr(bin_to_dec(i))

def bin_to_hex(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	if i.startswith("-"):
		i = re.sub("-", "", i)
		return "-" + str(hex(int(i, 2)))[2:]
	else:
		return str(hex(int(i, 2)))[2:]

def bnr_to_bin(i: str) -> str:
	return dec_to_bin(bnr_to_dec(i))

def bnr_to_hex(i: str) -> str:
	return dec_to_hex(bnr_to_dec(i))

def hex_to_dec(i: str) -> str:
	if not isinstance(i, str):
		raise TypeError("Not str input")
	return str(int(i, 16))

def hex_to_bin(i: str) -> str:
	return dec_to_bin(hex_to_dec(i))

def hex_to_bnr(i: str) -> str:
	return dec_to_bnr(hex_to_dec(i))

def main():
	print("num_trans_func work, for test")
	#print(float_to_hex_fp16("65496"))
	#print(float_to_hex_fp64("100"))
	#print(hex_fp64_to_float("4059000000000000"))
	#print(bnr_to_dec("110"))
	print(float_to_hex(65536))

if __name__ == '__main__':
	main()